<template>
    <div>
        <el-form label-width="85px" :model="Contract" ref="Contract_refs" :rules="rules">
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="合同编号" prop="ContractNumber">
                        <el-input v-model="Contract.ContractNumber" disabled></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="合同名称" prop="Title">
                        <el-input v-model="Contract.Title"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="合同类型一" prop="ContractTypeOne">
                        <el-select style="width: 100%" v-model="Contract.ContractTypeOne" filterable @change="selectchange_event($event,true)">
                            <el-option v-for="v in AllContractType" v-if="v.ParentId == 0" :label="v.Name" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="合同类型二" prop="ContractTypeTwo">
                        <el-select style="width: 100%" v-model="Contract.ContractTypeTwo" filterable>
                            <el-option :value="0" label="无"></el-option>
                            <el-option v-for="v in AllContractType_" :label="v.Name" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="客户信息" prop="CustomerId">
                        <el-select style="width: 100%" v-model="Contract.CustomerId" filterable>
                            <el-option v-for="v in AllCustomer" :label="v.CustomerCompany" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item style="width: 100%" label="标物名称" prop="NameSubjectMatter">
                        <el-input v-model="Contract.NameSubjectMatter"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="合同金额" prop="ContractPrice">
                        <el-input v-model="Contract.ContractPrice"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="签订日期" prop="SignDate">
                        <el-date-picker style="width: 100%"
                                v-model="Contract.SignDate"
                                type="date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="签订部门" prop="DepartmentId">
                        <el-select style="width: 100%" v-model="Contract.DepartmentId" filterable>
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="签订人" prop="SignUserId">
                        <el-select style="width: 100%" v-model="Contract.SignUserId" filterable>
                            <el-option v-for="v in $store.state.AllUsers" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="备注" prop="Remark">
                        <el-input v-model="Contract.Remark"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="归档日期" prop="FilinDate">
                        <el-date-picker style="width: 100%"
                                v-model="Contract.FilinDate"
                                type="date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>


            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="合同文件">
                        <UploadFileF ref="UploadFileF_AddContract"
                                     :multiple="false"
                                     :another-data="AnotherData"
                                     :showfilelist="true"
                                     :upshow="false"
                                     content="上传合同"
                                     btnsize="medium"></UploadFileF>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col align="center">
                    <el-button type="success" @click="AddContract_event">添加合同</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
    import * as IsExistjs from '../../../public/Scriput/IsExist'
    import UploadFileF from "../UploadFileF";

    export default {
        name: "AddContract",
        components: {
            UploadFileF,
        },
        props: {
            AllDepartment: {
                type: Array,
                required: true
            },
            AllProject: {
                type: Array,
                required: true
            },
            AllCustomer: {
                type: Array,
                required: true
            },
            AllContractType: {
                type: Array,
                required: true
            },
        },
        created() {
            IsExistjs.IsExist.http(this.$http, this.$message)
        },
        data() {
            var demicalcheck=(rule, value, callback)=>{
                if (!value){
                    return callback(new Error('金额不能为空'));
                }else{
                    let pattern = /^[0-9]+([.]{1}[0-9]+){0,1}$/;
                    if (pattern.test(value)) {
                        callback();
                    } else {
                        callback(new Error('必须数字'));
                    }
                }
            }
            return {
                Contract: {
                    ContractNumber: '',
                    Title: '',
                    FileName: '',
                    ContractTypeOne: '',
                    ContractTypeTwo: '',
                    CustomerId: '',
                    NameSubjectMatter: '',
                    ContractPrice: '',
                    SignDate: '',
                    DepartmentId: '',
                    SignUserId: '',
                    Remark: '',
                    FilinDate: '',
                    OperatId: 0,
                    OperatDate: new Date(),
                    InputDate: new Date(),
                    Delete: false,
                },
                //合同类型二
                AllContractType_: [],
                //文件上传参数
                AnotherData: {
                    FileType: 4,
                    id: 0,
                    Type: 0,
                    Single: true,
                },
                rules:{
                    ContractPrice:[
                        { validator: demicalcheck, trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            // 合同添加
            async AddContract_event() {
                let result = await IsExistjs.IsExist.IsExistContractNumber(this.Contract.ContractNumber)
                if (!result) {
                    if (this.$refs['UploadFileF_AddContract'].fileList.length > 0) {
                        this.Contract.FileName = this.$refs['UploadFileF_AddContract'].fileList[0].name
                    }
                    this.$refs["Contract_refs"].validate((vali)=>{
                        if (vali){
                            let loadding = this.openLoading();
                            this.$http.post('Contract/AddContractContent', { Contract: this.Contract} ).then(res => {
                                loadding.close();
                                if (res.data.result) {
                                    this.$refs['Contract_refs'].resetFields()
                                    this.$message.success('添加成功!')
                                    this.$parent.$parent.AddContract_dialog = false
                                    this.$parent.$parent.formdata()
                                    this.AnotherData.id = res.data.id
                                    if (this.$refs['UploadFileF_AddContract'].fileList.length > 0) {
                                        this.$refs['UploadFileF_AddContract'].submitUpload()
                                    }
                                } else {
                                    this.$message.warning('添加失败!')
                                }
                            })
                        }
                    })
                } else {
                    this.$message.info('此合同编号已存在,无法再次添加!')
                }
            },
            // 合同类型切换
            selectchange_event(val, result) {
                if (result) {
                    let ContractTypeLis = []
                    this.AllContractType.forEach(v => {
                        if (v.ParentId == val) {
                            ContractTypeLis.push(v)
                        }
                    })
                    this.AllContractType_ = ContractTypeLis
                }
                this.NewCustomerType(val)
                if (this.AllContractType_.length>0){
                    this.Contract.ContractTypeTwo=this.AllContractType_[0].Id
                }else {
                    this.Contract.ContractTypeTwo=0
                }
            },
            // 获取最新编号
            NewCustomerType(type) {
                this.$http.post('Contract/NewCustomerType', { type: type }).then(res => {
                    this.Contract.ContractNumber = res.data
                })
            },
        }
    }
</script>

<style scoped>
    .el-row {
        margin-top: 0px !important;
    }
</style>
